<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: GetCircle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\msg;


use app\api\model\dao\DashangRecord;
use app\api\model\dao\MsgRecords;
use app\api\model\dao\MsgUser;
use app\api\model\dao\NotePraise;
use app\api\model\dao\NoteReply;
use app\api\model\dao\NoteReplyReply;
use app\api\model\dao\UserAccount;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Validate;

class MsgIndex extends PageBase
{

    private $objNotePraise;
    private $objNoteReply;
    private $objNoteReplyReply;
    private $objNoteDashang;
    private $objMsgUser;
    private $objMsgRecords;
    private $objUserAccount;

    public function __construct()
    {
        parent::__construct();

        $this->objNotePraise = new NotePraise();
        $this->objNoteReply = new NoteReply();
        $this->objNoteReplyReply = new NoteReplyReply();
        $this->objNoteDashang = new DashangRecord();
        $this->objMsgUser = new MsgUser();
        $this->objMsgRecords = new MsgRecords();
        $this->objUserAccount = new UserAccount();

    }

    /**
     * page 入口
     *
     * @param $param
     * @return array
     * @throws Exception
     */
    public function excute($param)
    {
        $this->checkToken($param);

        $this->user_id = !empty($this->user_id) ? $this->user_id : 0;

        // 1、获取顶未读消息数
        $conds = [
            'note_userid' => ['=', $this->user_id],
            'is_delete' => ['=', Flag::CONST_NOTEPRIASE_DELETE_NOT],
            'status' => ['=', 0],
        ];
        $praise_cnt = $this->objNotePraise->getNotePraiseCountByConds($conds);
        empty($praise_cnt) && $praise_cnt = 0;

        // 2、获取打赏未读消息数
        $conds = [
            'note_userid' => ['=', $this->user_id],
            'status' => ['=', 0],
        ];
        $dashang_cnt = $this->objNoteDashang->getDashangCountByConds($conds);
        empty($dashang_cnt) && $dashang_cnt = 0;

        // 3、获取回复未读消息数
        $conds = [
            'recv_userid' => ['=', $this->user_id],
            'status' => ['=', 0],
        ];
        $reply_reply_cnt = $this->objNoteReplyReply->getNoteReplyReplyCountByConds($conds, 'reply_id');
        empty($reply_reply_cnt) && $reply_reply_cnt = 0;

        // 4、私信
//        $msg_records = $this->objMsgRecords->getMsgRecords(['recv_user_id'=>['=', $this->user_id]], $this->offset, $this->limit);
//
//        $userids = [];
//        $unread_msg_cnt = [];
//        $last_msg = [];
//        $user_account = [];
//
//        foreach ($msg_records as $val) {
//            $userids[] = $val['user_id'];
//        }
//
//        if (!empty($userids)) {
//            $user_account = $this->objUserAccount->getUserAccountByUserids($userids, ['nick_name', 'avatar']);
//            $unread_msg_cnt = $this->objMsgUser->getMsgUnReadCountBatch($this->user_id, $userids);
//            $last_msg = $this->objMsgUser->getLastMsgBatch($this->user_id, $userids);
//        }
//
//        $userid2UnReadMsgCnt = [];
//        $userid2LastMsg = [];
//        $userid2User = [];
//        foreach ($user_account as $val) {
//            $userid2User[$val['id']] = $val;
//        }
//        foreach ($unread_msg_cnt as $val) {
//            $userid2UnReadMsgCnt[$val['user_id']] = $val['cnt'];
//        }
//        foreach ($last_msg as $val) {
//            $userid2LastMsg[$val['user_id']] = $val['content'];
//        }
//
//        $msg = [];
//        foreach ($userids as $userid) {
//            $msg[] = [
//                'userid' => $userid,
//                'photo' => $userid2User[$userid]['avatar'],
//                'name' => $userid2User[$userid]['nick_name'],
//                'msg' => empty($userid2LastMsg[$userid]) ? '' : $userid2LastMsg[$userid],
//                'unread_count' => empty($userid2UnReadMsgCnt[$userid]) ? 0 : $userid2UnReadMsgCnt[$userid],
//            ];
//        }

        $this->data['praise_count'] = $praise_cnt;
        $this->data['dashang_count'] = $dashang_cnt;
        $this->data['reply_count'] = $reply_reply_cnt;
        $this->data['msg'] = [];

        return $this->getRes();
    }

}
